package com.dudy;

/**
 * @program: zhuguangDemo
 * @description: ${description}
 * @author: dudy
 * @create: 2018-04-02 19:45
 **/
public class JoinTest {
    private static class CutInLine implements Runnable {

        private Thread thread;

        public CutInLine(Thread thread) {
            this.thread = thread;
        }

        @Override
        public void run() {
            try {
                thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + " will work");
        }
    }

    public static void main(String[] args) {
        Thread thread = Thread.currentThread();
        for (int i = 0; i < 10; i++) {
            Thread thread1 = new Thread(new CutInLine(thread), String.valueOf(i));
            System.out.println(thread.getId()+" cut in thread " + thread1.getName());
            thread1.start();
            thread = thread1;
        }
    }
}
